package com.atguigu.tingshu.order.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ClassName: OrderReceiver
 * Package: com.atguigu.tingshu.order.receiver
 * Description:
 *
 * @Author 咚咚小圆帽
 * @Create 2025/8/4 Monday 23:21
 * @Version 1.0
 */
@Component
@Slf4j
public class OrderReceiver {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 监听到延迟关单的消息
     *
     * @param orderId 订单id
     * @param message 消息
     * @param channel 队列
     */
    @SneakyThrows
    @RabbitListener(queues = MqConst.QUEUE_CANCEL_ORDER)
    public void cacelOrder(Long orderId, Message message, Channel channel) {
        if(orderId!=null){
            log.info("监听到延迟关单消息，orderId:{}", orderId);
            orderInfoService.cancelOrder(orderId);
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }

}
